// pages/me/love/index.js
import util from '../../../utils/utils';
import data from '../../../utils/data';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    initUser:true,
    listData:[],
    listPage: 1,
    toTop:false,
    listEmpty: false,
    listMore: true,
  },
  initPage: function(){
    // let self = this;
    // wx.hideShareMenu();
    // self.initData();
  },
  onShow: function () {
    console.log('>>>>> love pages onShow <<<<<')
    let self = this;
    wx.hideShareMenu();
    self.initData(true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if(this.data.initUser !== true){
      return false;
    }
    this.initData(true);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let self = this;
    if (self.data.listMore === true) {
      data.getLove(function(result) {
        let setData = {
          listPage:self.data.listPage + 1,
          listMore:false
        };
        let index  = self.data.listData.length;
        let _data  = data.formatGoodsList(result.data);
        let _total = result.total;
        let more   = false;
        for (let i = 0; i < _data.length; i++) {
          setData['listData[' + (index++) + ']'] = _data[i];
        }
        if (index < _total) {
          more = true;
        }
        setData['listMore'] = more;
        self.setData(setData);
      }, self.data.listPage + 1, true);
    }
  },
  // goodscell 点击
  onNavToPage: function (event) {
    let { id, platform, buytype } = event.currentTarget.dataset
    if (buytype === 2) {
      wx.navigateTo({
        url: `/pages/goods/seckillGoodsDetails/seckillGoodsDetails?id=${id}`
      })
    } else if (platform === 4) {
      wx.navigateTo({
        url: `/pages/goods/mmallGoodsDetails/mmallGoodsDetails?id=${id}`
      })
    } else {
      wx.navigateTo({
        url: `/pages/goods/detail/detail?id=${id}`
      })
    }
  },
  onClose: function (event){
    // 滑动的模块返回
    /**
     * 发起网络请求删除
     * 本地列表移除
     */
    let id = event.target.dataset.id;
    let self = this;
    let instance = event.detail.instance;
    instance.close();
    // 发起网络请求
    if(id){
      util.request({
        url:util.api.delFollow,
        data:{
          good_ids:[
              id
          ]
        },
        // 修改本地数据
        callback:function (result) {
          let data = self.data.listData;
          let tempData = [];
          for(let i=0;i<data.length;i++){
            if(data[i]['id'] !== id){
              tempData.push(data[i]);
            }
          }
          util.cache_remove('my_love');
          self.setData({
            listData:tempData
          });
        }
      });
    }
  },
  onPageScroll:function(e){
    let self  = this;
    let scrollTop = e.scrollTop;
    let backTopValue = scrollTop > 200;
    if(backTopValue !== self.data.toTop){
      self.setData({
        toTop: backTopValue
      });
    }
  },
  initData: function (ref) {
    let self = this;
    let update = ref || false;
    wx.showNavigationBarLoading();
    /**
     * 拉取基础数据资源
     */
    data.getLove(function (result) {
      let _data = data.formatGoodsList(result.data);
      let more = false;
      if (_data.length === result.total) {
        more = true;
      }
      self.setData({
        listData: _data,
        listPage:1,
        listEmpty:_data <= 0,
        listMore:more
      });
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1, update);
  }
});
